package rbtree;

import bst.BSTNode;

/**
 * Projeto : Arvore Preta e Vermelha
 * 
 * Equipe:
 * 			Antonio Eduardo
 * 			Gabriel de Brito
 * 			Gervasio Costa
 * 			Jordan Ferreira
 * 			Wesley Breno
 * 
 */

public class RBNode<T extends Comparable<T>> extends BSTNode<T> {

	public enum Color {BLACK, RED}; // necessario ser public para mudança correta de cores na interface
	protected Color color;

	public RBNode() {
		super();
		this.color = Color.BLACK;
	}

	public Color getColor(){
		return this.color;
	}

	public void setColor(){
		if(getColor() == Color.BLACK) color = Color.RED;
		else{
			color = Color.BLACK;
		}
	}
	
	protected RBNode<T> getUncle(){
		if(this.getParent().equals(this.getParent().getParent().getLeft())){ // Se o pai for filho a esquerda
				return (RBNode<T>) this.getParent().getParent().getRight();
		} else { // Se o pai for filho a direita
			return (RBNode<T>) this.getParent().getParent().getLeft();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj){
		boolean resp = super.equals(obj);
		if ( resp && !((BSTNode<T>) obj).isEmpty() )
			resp = this.color == ((RBNode<T>) obj).getColor();
		return resp;
	}

	@Override
	public String toString(){
		String resp = "NIL";
		if(!this.isEmpty()){
			resp = "(" + data.toString();
			if(color == Color.RED){
				resp += ", R)";
			} else {
				resp += ", B)";
			}
		} return resp;
	}

}